﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBAccess;
using SSPharma.Common;
using SSPharma.Model;
using System.Data.Common;
using SSPharma.Biz.Common;
using System.Transactions;

namespace SSPharma.Biz.DanhMuc
{
    class BizDanhMucNghiepVu
    {

        //khai báo hàm Common dùng để lấy các hàm dùng chung
        private BizCommon _BizCommon = new BizCommon();
        /// <summary>
        /// User: TAMNN 01/08/2014
        /// lấy nghiệp vụ theo mã nghiệp vụ
        /// </summary>
        /// <param name="Ma"></param>
        /// <returns></returns>
        public DataTable ft_GetDataDanhMucNghiepVuTheoMa(string Ma)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" SELECT a.*,b.TenDonVi from HT_DM_NghiepVu a with(nolock) left outer join HT_DM_DonVi b with(nolock) on a.MaDonVi = b.MaDonVi where MaNghiepVu = @Ma");
            SQLServer.Param[] Params = new SQLServer.Param[1];
            Params[0] = new SQLServer.Param("@Ma", Ma);
            return Global.DataFactory.GetDataTable(strSQL.ToString(), Params);
        }
        /// <summary>
        /// User: TAMNN 01/08/2014
        /// Lay cau truc de luu du lieu xong database
        /// </summary>
        /// <returns></returns>
        public DataSet fs_LayCauTrucTable()
        {
            DataSet dsdata = new DataSet();
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" SELECT a.*,b.TenDonVi from HT_DM_NghiepVu a with(nolock) left outer join HT_DM_DonVi b with(nolock) on a.MaDonVi = b.MaDonVi where 1=2");
            DataTable dt_NghiepVu = Global.DataFactory.GetDataTable(strSQL.ToString());
            dt_NghiepVu.TableName = "dt_NghiepVu";
            dsdata.Tables.Add(dt_NghiepVu);
            return dsdata;
        }
        /// <summary>
        /// User: TAMNN 01/08/2014
        /// Them danh muc nghiệp vụ
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="TienTo"></param>
        public void fg_InsertDanhMucNghiepVu(DataTable dt)
        {
            try
            {
                string query_NghiepVu = "Select * from HT_DM_NghiepVu where 1 = 2";
                using (TransactionScope scope = new TransactionScope())
                {
                    Global.DataFactory.RunSaveData(dt, query_NghiepVu);
                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw new System.Configuration.Provider.ProviderException(ex.Message);
            }
        }
        /// <summary>
        /// User: TAMNN 01/08/2014
        /// Cap nhat danh muc nghiệp vụ
        /// </summary>
        /// <param name="dt"></param>
        public void fg_UpdateDanhMucNghiepVu(DataTable dt)
        {
            try
            {
                string query_NghiepVu = "Select * from HT_DM_NghiepVu where 1 = 2";
                using (TransactionScope scope = new TransactionScope())
                {
                    Global.DataFactory.RunSaveData(dt, query_NghiepVu);
                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw new System.Configuration.Provider.ProviderException(ex.Message);
            }
        }
        /// <summary>
        /// User: TAMNN 01/08/2014
        /// Mục đích: lấy danh sách theo danh mục
        /// </summary>
        /// <returns></returns>
        public DataTable ft_GetListdata(IList<string> list, string Orderby)
        {
            string str = string.Join(",", list.ToArray());
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" SELECT ");
            strSQL.Append(str);
            strSQL.Append(" from HT_DM_NghiepVu with(nolock)");
            strSQL.Append(!string.IsNullOrEmpty(Orderby) ? (" order by " + Orderby) : "");
            return Global.DataFactory.GetDataTable(strSQL.ToString());
        }

        /// <summary>
        /// User:TAMNN 01/08/2014
        /// Mục đích: kiểm tra nghiệp vụ đã tồn tại hay chưa
        /// </summary>
        /// <param name="p_sTableName"></param>
        /// <param name="p_sfill"></param>
        /// <param name="p_sGiatri"></param>
        /// <param name="p_sDieuKien"></param>
        /// <returns></returns>
        public string fs_CheckDataExistsNghiepVu(string p_sGiatri)
        {
            return _BizCommon.fs_LayDuLieuTrongTable("HT_DM_NghiepVu", "MaNghiepVu", "MaNghiepVu  = N'" + p_sGiatri +"'" );
        }

        /// <summary>
        /// User: TAMNN 31/07/2014
        /// Mục đích: lấy cbo phạm vi
        /// </summary>
        /// <returns></returns>
        public IList<cboCommon> fl_DSDanhMucNhapXuat()
        {
            List<cboCommon> items = new List<cboCommon>() 
            {
                new cboCommon()
                {
                    Id = "N",
                    Name = "Nhập"
                },
                 new cboCommon()
                {
                    Id = "X",
                    Name = "Xuất"
                }
            };
            return items;
        }
    }
}
